package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.POSLogic;
import com.hotel.BL.POSTableLogic;

import data.POS;
import data.POSTable;
import data.POSZone;
import data.passingData.ResultData;
@SuppressWarnings("serial")
public class POSTableService extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("getAllTable")){
				getAllTable(req,resp);
			}else if(doClass.equals("getAllZone")){
				getAllZone(req,resp);
			}else if(doClass.equals("getAllPOS")){
				getAllPOS(req,resp);
			}else if(doClass.equals("getAllPOSLimit")){
				getAllZoneLimit(req,resp);
			}else if(doClass.equals("getAllTableInZone")){
				getAllTableInZone(req,resp);
			}else if(doClass.equals("getZoneById")){
				getZoneById(req,resp);
			}else if(doClass.equals("addZone")){
				AddZone(req,resp);
			}else if(doClass.equals("editZone")){
				EditZone(req,resp);
			}else if(doClass.equals("editStatusPOSZone")){
				ChangeStatusZone(req,resp);
			}else if(doClass.equals("delZone")){
				DeleteZone(req,resp);
			}else if(doClass.equals("getTableById")){
				getTableById(req,resp);
			}else if(doClass.equals("addTable")){
				AddTable(req,resp);
			}else if(doClass.equals("editTable")){
				EditTable(req,resp);
			}else if(doClass.equals("delTable")){
				DeleteTable(req,resp);
			}else if(doClass.equals("editStatusPOSTable")){
				ChangeStatusTable(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getAllTable(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<POSTable> table = new POSTableLogic(req,resp).GetAllPOSTable(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(table));
	}
	
	public void getAllZone(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String posId = (String) req.getParameter("posId");
		List<POSZone> zone = new POSTableLogic(req,resp).GetAllPOSZonebyPosId(hotelId,Long.parseLong(posId));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(zone));
	}
	
	public void getAllZoneLimit(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String posId = (String) req.getParameter("posId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		POSTableLogic posTableLogic = new POSTableLogic(req,resp);
		List<POSZone> obj = posTableLogic.GetAllPOSZoneLimit(hotelId,offset,line,posId);
		List<HashMap<String,Object>> zone = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<obj.size();i++){
			int zonesize = posTableLogic.GetAllPOSTableInZone(hotelId,obj.get(i).getId()).size();
			HashMap<String,Object> zoneset = new HashMap<String,Object>();
			zoneset.put("POSId", obj.get(i).getPOSId());
			zoneset.put("avaliable", obj.get(i).isAvaliable());
			zoneset.put("hotelId", obj.get(i).getHotelId());
			zoneset.put("id", obj.get(i).getId());
			zoneset.put("status", obj.get(i).isStatus());
			zoneset.put("title", obj.get(i).getTitle());
			zoneset.put("tablesize", zonesize);
			zone.add(zoneset);
		}
		double dataSize = new POSTableLogic(req,resp).GetAllPOSZoneSize(hotelId,posId).size();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,zone);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}
	
	public void getAllPOS(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<POS> pos = new POSLogic(req,resp).GetAllPOS(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(pos));
	}
	
	public void getAllTableInZone(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long zoneId = Long.parseLong(req.getParameter("zoneId"));
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		List<POSTable> obj = new POSTableLogic(req,resp).GetAllPOSTableInZoneLimit(hotelId,offset,line,zoneId);
		int dataSize = new POSTableLogic(req,resp).GetAllPOSTableInZone(hotelId,zoneId).size();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,obj);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}
	
	public void getZoneById(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		long posId = Long.parseLong(req.getParameter("id"));
		POSZone zone = new POSTableLogic(req,resp).GetPOSZone(posId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(zone));
	}
	
	private void AddZone(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		Long posId = Long.parseLong(req.getParameter("posId"));
		Object obj = new POSTableLogic(req,resp).AddZone(hotelId,title,posId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	public void getTableById(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		long tableId = Long.parseLong(req.getParameter("id"));
		POSTable table = new POSTableLogic(req,resp).GetPOSTable(tableId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(table));
	}
	
	private void AddTable(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String zonename = (String) req.getParameter("zonename");
		String nametable = (String) req.getParameter("nametable");
		String description = (String) req.getParameter("description");
		String typeAdd = (String) req.getParameter("typeAdd");
		POSTableLogic posTableLogic = new POSTableLogic(req,resp);
		Object obj = null;
		if(typeAdd!=null){
			if(typeAdd.equals("typeComma")){
				String[] arrayName = nametable.split(",");
				for(int i =0;i<arrayName.length;i++){
					obj = posTableLogic.AddTable(hotelId,zonename,arrayName[i],description);
				}
			}else if(typeAdd.equals("typePrefix")){
				String prefix = (String) req.getParameter("prefix");
				long sumTable = Long.parseLong(req.getParameter("sumTable"));
				long to = Long.parseLong(req.getParameter("to"));
				for(long i = sumTable;i<=to;i++){
					String nametableSet = prefix+String.valueOf(i);
					obj = posTableLogic.AddTable(hotelId,zonename,nametableSet,description);
				}
				
			}else if(typeAdd.equals("s")){
				obj = posTableLogic.AddTable(hotelId,zonename,nametable,description);
			}
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditTable(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long tableId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("nametable");
		Object obj = new POSTableLogic(req,resp).EditTable(tableId,title);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditZone(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long zoneId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		Object obj = new POSTableLogic(req,resp).EditZone(zoneId,title);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatusZone(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long zoneId = Long.parseLong(req.getParameter("zoneId"));
		Object obj = new POSTableLogic(req,resp).ChangeStatus(zoneId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatusTable(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long tableId = Long.parseLong(req.getParameter("tableId"));
		Object obj = new POSTableLogic(req,resp).ChangeStatusTable(tableId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void DeleteZone(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String zoneIdList = req.getParameter("zoneId");
		for(int i = 0; i < zoneIdList.split(",").length ; i++){
			new POSTableLogic(req,resp).DeleteZone(Long.parseLong(zoneIdList.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
	
	private void DeleteTable(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String tableIdList = req.getParameter("tableId");
		for(int i = 0; i < tableIdList.split(",").length ; i++){
			new POSTableLogic(req,resp).DeleteTable(Long.parseLong(tableIdList.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
}